<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Sub property expander
 * 
 * Expands the property predicate forms with the sub properties from the schema.
 */
class entityresolution_datafilter_SubPropertyExpander implements entityresolution_DataFilter
{
	/**
	 * RDF schema document.
	 * @var simplerdf_RdfSchemaDocument
	 */
	protected $rdfSchemaDocument;
	
	/**
	 * Create a new sub property expander instance.
	 * @param simplerdf_RdfSchemaDocument $rdfSchemaDocument RDF schema document
	 */
	public function __construct($rdfSchemaDocument)
	{
		$this->rdfSchemaDocument = $rdfSchemaDocument;
	}
	
	/**
	 * Filter the data of an entity.
	 * @param entityresolution_Entity $entity
	 */
	public function filterData($entity)
	{
		$properties = $entity->getProperties();
		foreach($properties as $property)
		{
			/* @var $property entityresolution_Property */
			
			$predicateFormsExpanded = array();

			// expand all predicate forms with the sub properties in from the schema
			$predicateForms = $property->getPredicateForms();
			foreach($predicateForms as $predicate)
			{
				$predicateFormsExpanded[] = $predicate;
				$predicateFormsExpanded = array_merge($predicateFormsExpanded, $this->rdfSchemaDocument->getSubPropertyUriList($predicate));
			}
			
			$predicateFormsExpanded = array_unique($predicateFormsExpanded);
			
			$property->setPredicateForms($predicateFormsExpanded);
		}
		$entity->setProperties($properties);
	}
}